package com.web4code.codegen.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.web4code.codegen.utils.serialize.YamlSerializer;

import org.apache.commons.io.IOUtils;

/**
 * 代码生成配置项
 * 
 * @author web4code
 */
public class GenConfig {
    private String dataSourceType = "local";
    private String dataSourceParam = ".filesource";
    private String templatePath = ".template";
    private String generatePath = ".code";
    private List<GenProjectTemplate> templates;

    private static GenConfig _instance = null;

    public static GenConfig getGenConfig() {
        if (_instance == null) {
            _instance = new GenConfig();
        }
        return _instance;
    }

    public static void loadConfigFile(final String configFile) {
        try {
            InputStream is = new FileInputStream(configFile);
            final String fileContent = IOUtils.toString(is, "utf-8");
            GenConfig genConfig = YamlSerializer.toObject(fileContent, GenConfig.class);
            _instance = genConfig;
        } catch (IOException e) {
            // OK ignore
        }
    }
    
    public String getDataSourceType() {
        return dataSourceType;
    }

    public void setDataSourceType(String dataSourceType) {
        this.dataSourceType = dataSourceType;
    }

    public String getDataSourceParam() {
        return dataSourceParam;
    }

    public void setDataSourceParam(String dataSourceParam) {
        this.dataSourceParam = dataSourceParam;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    public String getGeneratePath() {
        return generatePath;
    }

    public void setGeneratePath(String generatePath) {
        this.generatePath = generatePath;
    }

    public List<GenProjectTemplate> getTemplates() {
        return templates;
    }

    public void setTemplates(List<GenProjectTemplate> templates) {
        this.templates = templates;
    }
}
